//User.java
package memento.blackbox;


/**
 *@author:youtubezou
 *@version:2012-8-16
 */
public class User
{
	private String name;
	private String state = "Init state";
	private MementoKeeper mementoKeeper = new MementoKeeper();
	
	public User(String name)
	{
		this.name = name;
	}
	
	public String getState()
	{
		return state;
	}
	
	public void setState(String state)
	{
		this.state = state;
	}
	
	public void saveState()
	{
		mementoKeeper.pushMemento(new WideMemento(state));
	}
	
	public void restoreState()
	{
		WideMemento wideMemento = (WideMemento)mementoKeeper.popMemento();
		if(wideMemento != null) {
			state = (String)wideMemento.getState();
		}
	}
	
	public Memento peekMemento()
	{
		return mementoKeeper.peekMemento();
	}
	
	public String toString()
	{
		return name + " is " + state;
	}

	/**
	 * wide interface for User to access to memento. 
	 */
	private class WideMemento implements Memento
	{
		private Object state;

		private WideMemento(Object state)
		{
			this.state = state;
		}
		
		private Object getState()
		{
			return this.state;
		}
		
/*		private void setState(Object state)
		{
			this.state = state;
		}*/
	}
}
